define(['amazeui', 'jquery', 'global/url', 'global/constants'],
  function(amazeui, jquery, url, constants) {
    return ['$scope', '$rootScope', 'UtilService', 'MissionService', 'AdminService', function($scope, $rootScope, UtilService, MissionService, AdminService) {

        var params = {
        	'accessToken': $scope.accessToken,
        	'filter': {
	        	'pagesize': 10,
	        	'pno': 1,
	        	'search': ''
	        }
        }

        $scope.searchMissions = function (event) {
            console.log(event);
        	params.filter.search = $scope.search;
        	params.filter.pagesize = $scope.missions.pagesize
        	_getParentList(params);
        } 

        $scope.forwardPage = function (event) {
        	var pno = event.target.text;
        	params.filter.search = $scope.search;
        	params.filter.pagesize = $scope.missions.pagesize;
        	params.filter.pno = pno;
        	_getParentList(params);
        }

        $scope.prePage = function () {
        	var currentPage = $scope.missions.currentPage;
        	if (currentPage === 1) {
        		return;
        	}
        	params.filter.search = $scope.search;
        	params.filter.pagesize = $scope.missions.pagesize;
        	params.filter.pno = currentPage - 1;
        	_getParentList(params);
        }

        $scope.nextPage = function () {
        	var currentPage = $scope.missions.currentPage;
        	if (currentPage === $scope.missions.totalPagers) {
        		return;
        	}
        	params.filter.search = $scope.search;
        	params.filter.pagesize = $scope.missions.pagesize;
        	params.filter.pno = currentPage + 1;
        	_getParentList(params);
        }

	    function _getParentList (params) {
	    	
	    	MissionService.listMissions(params).success(function(response, status) {
	  		    if (response.jsonResponse.statusCode == constants.responseCode.SUCCESS) {
	  		       $scope.missions = response.jsonResponse.response.data;
	  	        } else if (response.statusCode == constants.responseCode.LOGIN_ANOTHER_DEVICE) {
	  	           AdminService.Admin.logout();
	  	           window.location.href = constants.pages.login;
	  	        }
            });
	    }

        function _initQuestTypes () {
            MissionService.listQuestTypes({'accessToken': $scope.accessToken}).success(function(response, status){
                if (response.jsonResponse.statusCode == constants.responseCode.SUCCESS) {
                    if (response.jsonResponse.response.meta.code == 200) {
                        $scope.questTypes = response.jsonResponse.response.data;
                        $scope.quest = {};
                        $scope.quest.missionType = $scope.questTypes[0].typeId;
                    }
                } else if (response.statusCode == constants.responseCode.LOGIN_ANOTHER_DEVICE) {
                    AdminService.Admin.logout();
                    window.location.href = constants.pages.login;
                }
            });
        }


	   UtilService.renderSelect();
	   _getParentList(params);
       _initQuestTypes();

    }];
  }
)
